package com.kongzue.baseokhttpv3.newslice;

import com.kongzue.baseokhttp.util.BaseOkHttp;
import com.kongzue.baseokhttp.util.BaseOkHttpConfig;
import com.kongzue.baseokhttp.util.Parameter;
import com.kongzue.baseokhttpv3.ResourceTable;
import com.kongzue.baseokhttpv3.util.JsonUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import org.apache.http.util.TextUtils;

import java.util.Map;

public class SetAbilitySlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_seting_layout);
        initView();
        initClick();
    }

    private TextField cookie;

    private void initClick() {
    }

    private void initView() {
        Text yes = (Text) findComponentById(ResourceTable.Id_yes);
        Text no = (Text) findComponentById(ResourceTable.Id_no);
        Text Repeatedyes = (Text) findComponentById(ResourceTable.Id_Repeated_yes);
        Text Repeatedno = (Text) findComponentById(ResourceTable.Id_Repeated_no);
        Text save_url1 = (Text) findComponentById(ResourceTable.Id_save_url1);
        Text save_url2 = (Text) findComponentById(ResourceTable.Id_save_url2);
        TextField url = (TextField) findComponentById(ResourceTable.Id_url);
        TextField url2 = (TextField) findComponentById(ResourceTable.Id_url2);
        Text save = (Text) findComponentById(ResourceTable.Id_save);

        StringBuffer sb = new StringBuffer();
        if (BaseOkHttpConfig.getInstance().getReserveServiceUrls() != null) {
            for (String str : BaseOkHttpConfig.getInstance().getReserveServiceUrls()) {
                sb.append(str);
                sb.append(",");
            }
            String str = sb.toString();
            str = str.substring(0,str.length()-1);
            url2.setText(str);
        }

        if (BaseOkHttpConfig.getInstance().getServiceUrl() == null || BaseOkHttpConfig.getInstance().getServiceUrl().equals("")) {
            BaseOkHttpConfig.getInstance().setServiceUrl("https://www.fastmock.site");
        }

        url.setText(BaseOkHttpConfig.getInstance().getServiceUrl());
        save.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BaseOkHttpConfig.getInstance().setServiceUrl(url.getText());
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("ok").setDuration(1000).show();

            }
        });

        //全局日志开关
        yes.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BaseOkHttpConfig.getInstance().setDebugMode(true);
                BaseOkHttpConfig.getInstance().setDetailsLogs(true);
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("ok").setDuration(1000).show();

            }
        });

        no.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BaseOkHttpConfig.getInstance().setDebugMode(false);
                BaseOkHttpConfig.getInstance().setDetailsLogs(false);
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("ok").setDuration(1000).show();

            }
        });

        //重复请求开关
        Repeatedyes.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BaseOkHttpConfig.getInstance().setDisallowSameRequest(true);
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("ok").setDuration(1000).show();

            }
        });

        Repeatedno.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                BaseOkHttpConfig.getInstance().setDisallowSameRequest(false);
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("ok").setDuration(1000).show();

            }
        });

        //设置全局请求头消息
        cookie = (TextField) findComponentById(ResourceTable.Id_cookie_input);

        if (BaseOkHttpConfig.getInstance().getOverallHeader() == null) {
            cookie.setText("{\"cookie\": \"177c070d1922f938ccee95b6bd04df0b\"}");
        }else{
            cookie.setText(BaseOkHttpConfig.getInstance().getOverallHeader().toParameterJson().toString());
        }

        Text savecookie = (Text) findComponentById(ResourceTable.Id_savecookie);

        savecookie.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                setCookie();
            }
        });

        save_url1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                if (url2.getText().equals("") || url2.getText() == null) {
                    BaseOkHttpConfig.getInstance().setReserveServiceUrls(null);
                } else {
                    String[] srt = url2.getText().split(",");
                    BaseOkHttpConfig.getInstance().setReserveServiceUrls(srt);
                }

                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("save ok").setDuration(1000).show();

            }
        });
        save_url2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
                toastDialog.setText("clean ok").setDuration(1000).show();
                //容灾地址
                BaseOkHttpConfig.getInstance().setReserveServiceUrls(null);
                url2.setText("");
            }
        });
    }


    public void setCookie() {
        Parameter parameter = new Parameter();
        if (!TextUtils.isEmpty(cookie.getText().toString())) {
            String json = cookie.getText().toString();
            Map<String, Object> jsonObject = null;
            try {
                jsonObject = JsonUtil.jsonToMap(json);
                for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                    parameter.add(entry.getKey(), entry.getValue().toString());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            ToastDialog toastDialog = new ToastDialog(this);
            toastDialog.setText("header is null").setDuration(1000).show();
        }
        BaseOkHttpConfig.getInstance().setOverallHeader(parameter);
        ToastDialog toastDialog = new ToastDialog(SetAbilitySlice.this);
        toastDialog.setText("OK").setDuration(1000).show();
    }
}
